﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Sale_Operation;
using Sale_Common;
using SaleSystem;
using Sale_Model;

namespace SaleSystem.BaseData
{
    public partial class frm_ProductInfoList : PageBase
    {

        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.bn_AddProduct.OnClientClick = win_Content2.GetShowReference("frm_ProductInfoEdit.aspx?Id=0","新增产品");
                this.bn_DeleteProduct.OnClientClick = gv_List.GetNoSelectionAlertInParentReference("您至少选择一项产品才能删除！");
                BindSort(); 
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == "refreshSort")
            {
                BindSort();
            } 
        
        }
   
        protected void tv_Menu_NodeCommand(object sender, ExtAspNet.TreeCommandEventArgs e)
        {
            ExtAspNet.Alert.Show("OK");
        }

        protected void tv_Menu_NodeCheck(object sender, ExtAspNet.TreeCheckEventArgs e)
        { 

        } 
 
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            GetAndBind();
        }

        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                Sale_Model.ProductInfo SM = new Sale_Model.ProductInfo();

                SM.PInfo_Id = ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                SM.PInfo_Name = ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                bool blIsUsing = LinkService.CheckIsUsing("ProductInfo", SM.PInfo_Id.ToString());
                if (!blIsUsing)
                {

                    bool blDelete = Manager.Delete(SM);
                    if (blDelete)
                    {
                        GetAndBind();
                        ExtAspNet.Alert.Show("删除完成！", ExtAspNet.MessageBoxIcon.Information);
                    }
                    else
                        ExtAspNet.Alert.Show("删除失败！", ExtAspNet.MessageBoxIcon.Error);
                }
                else
                    ExtAspNet.Alert.Show("该数据项正在使用，不能删除！", ExtAspNet.MessageBoxIcon.Warning);
            }

            else if (e.CommandName.ToLower() == "edit")
            {
                win_Content2.Hidden = false;
                win_Content2.IFrameUrl = "frm_ProductInfoEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                win_Content2.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);

            }
        }
        protected void bn_DeleteProductClick(object sender, EventArgs e)
        {
            int[] indexs= gv_List.SelectedRowIndexArray;
            StringBuilder sbNodelete = new StringBuilder();
            StringBuilder sbDelete = new StringBuilder();
            int iDelteCount = 0;
            for (int i = 0; i < indexs.Length; i++)
            {
                Sale_Model.ProductInfo SM = new Sale_Model.ProductInfo();

                SM.PInfo_Id = ValueHandler.GetIntNumberValue(gv_List.DataKeys[indexs[i]][0]);
                SM.PInfo_Name = ValueHandler.GetStringValue(gv_List.DataKeys[indexs[i]][1]);
                bool blIsUsing = LinkService.CheckIsUsing("ProductInfo", SM.PInfo_Id.ToString());
                if (!blIsUsing)
                {

                    bool blDelete = Manager.Delete(SM);
                    if (blDelete)
                    {
                        sbDelete.Append(SM.PInfo_Name + ",");
                        iDelteCount++;
                    }
                }
                else
                {
                    sbNodelete.Append(SM.PInfo_Name + ",");
                }
            }
            if (sbDelete.Length > 0)
                GetAndBind();
            if (sbNodelete.Length > 0)
            {
                ExtAspNet.Alert.ShowInTop("已经删除的产品信息为：" + sbDelete.ToString() + "！<br>" + "正在使用无法删除的为：" + sbNodelete.ToString() + "！");
            }
            else
            {
                ExtAspNet.Alert.ShowInTop("已经删除<font color='red'>(" + iDelteCount.ToString() + ")</font>个产品！");
            }
        }
        protected void gv_List_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
            GetAndBind();
        }
        protected void gv_List_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
 

        }
        protected void bn_ReloadClick(object sender, EventArgs e)
        {
            BindSort();
        }
        protected void bn_ReloadProductClick(object sender, EventArgs e)
        {
            GetAndBind() ;
        }
        protected void bn_AddClick(object sender, EventArgs e)
        { 
            if(tv_Menu.SelectedNodeIDArray.Length>0)
            {
            win_Content.Hidden = false;
            win_Content.IFrameUrl = "frm_ProductSortEdit.aspx?PSort_PSort_ID=" + tv_Menu.SelectedNodeIDArray[0] ;
     
            }
            else
            {
                ExtAspNet.Alert.Show("请选择某个产品大类后再做此操作！", ExtAspNet.MessageBoxIcon.Warning);
            }
        }
        protected void bn_EditClick(object sender, EventArgs e)
        {
            if (tv_Menu.SelectedNodeIDArray.Length > 0)
            {
                win_Content.Hidden = false;
                win_Content.IFrameUrl = "frm_ProductSortEdit.aspx?Id=" + tv_Menu.SelectedNodeIDArray[0];
                 ExtAspNet.TreeNode tn = tv_Menu.FindNode(tv_Menu.SelectedNodeIDArray[0]);
                 win_Content.Title = "编辑产品类别";
            }
        }
        protected void bn_DeleteClick(object sender, EventArgs e)
        {
            if (tv_Menu.SelectedNodeIDArray.Length <= 0)
            {
                ExtAspNet.Alert.ShowInTop("请选择您要删除的类别！", ExtAspNet.MessageBoxIcon.Information);            
            }
            else
            {

                Sale_Model.ProductSort SM = new Sale_Model.ProductSort();
                SM.PSort_ID = ValueHandler.GetIntNumberValue(tv_Menu.SelectedNodeIDArray[0]);
                SM = SortManager.GetModel(SM) as ProductSort;
                if (SM.PSort_Type == 1)
                {
                    ExtAspNet.Alert.ShowInTop("此类别为系统初始化类别，无法删除！", ExtAspNet.MessageBoxIcon.Warning);
                }
                else
                {

                    bool blIsUsing = LinkService.CheckIsUsing("ProductSort", SM.PSort_ID.ToString());
                    if (!blIsUsing)
                    {

                        bool blDelete = SortManager.Delete(SM);
                        if (blDelete)
                        {
                            BindSort();
                            ExtAspNet.Alert.ShowInTop("删除完成！", ExtAspNet.MessageBoxIcon.Information);
                        }
                        else
                            ExtAspNet.Alert.ShowInTop("删除失败！", ExtAspNet.MessageBoxIcon.Error);
                    } 
                    else
                        ExtAspNet.Alert.ShowInTop("该类别下有子类或者产品，不能删除！", ExtAspNet.MessageBoxIcon.Warning);
                }
            }  
        }

        #endregion

        #region Method

        private void GetAndBind()
        {
            FormatGrid(gv_List);
            InitCondition();
            dtList = Manager.GetList(strCondition, gv_List.PageIndex + 1, ref iRecordCount);
            gv_List.DataSource = dtList;
            gv_List.DataBind();
            gv_List.RecordCount = iRecordCount;
        }
        private void BindSort()
        {
            tv_Menu.Nodes.Clear();
            DataTable dtSort = SortManager.GetList("");
            ExtAspNet.TreeNode tn = new ExtAspNet.TreeNode();
            tn.NodeID = "0"; 
            tn.Text = "<a href='#' onclick=\"__doPostBack('','" + LinkService.RefreshString + "');\">公司产品库</a>"; 
            tv_Menu.Nodes.Add(tn);
            BindTree(dtSort, tn.Nodes, 0);
            tv_Menu.Nodes[0].Expanded = true;
            //设置类别是否leaf
            SetLeaf(tv_Menu.Nodes); 
        }
        private void SetLeaf(ExtAspNet.TreeNodeCollection tns)
        {
            for (int i = 0; i < tns.Count; i++)
            {
                if (tns[i].Nodes.Count > 0)
                {
                    tns[i].Leaf = false;
                    SetLeaf(tns[i].Nodes);
                }
                else
                {
                    tns[i].Leaf = true;
                }
            }
        }
        private void BindTree(DataTable dt, ExtAspNet.TreeNodeCollection tns, int id)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "PSort_PSort_ID="+id;
            for (int i = 0; i < dv.Count; i++)
            {
                ExtAspNet.TreeNode tn = new ExtAspNet.TreeNode();
                tn.NodeID = ValueHandler.GetStringValue(dv[i]["PSort_ID"]);
                tn.Text = "<a href='#' onclick=\"__doPostBack('','"+LinkService.RefreshString+"');\">" + ValueHandler.GetStringValue(dv[i]["PSort_Name"]) + "</a>";
                
                tn.EnablePostBack = false;
                tn.Expanded = true;
                tns.Add(tn); 
                tn.Icon = ExtAspNet.Icon.Folder;
                BindTree(dt,tn.Nodes,ValueHandler.GetIntNumberValue(dv[i]["PSort_ID"]));
            }
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        public override void InitCondition()
        {
            if (tb_PInfo_CH.Text.Trim() != "")
            {
                strCondition += " PInfo_CH LIKE '%" + ValueHandler.GetStringValue(tb_PInfo_CH.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (tb_PInfo_Code.Text.Trim() != "")
            {
                strCondition += " PInfo_Code LIKE '%" + ValueHandler.GetStringValue(tb_PInfo_Code.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            } 
            if(tb_PInfo_Name.Text.Trim() != "")
            {
                strCondition += " PInfo_Name LIKE '%" + ValueHandler.GetStringValue(tb_PInfo_Name.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (tb_PInfo_Number.Text.Trim() != "")
            {
                strCondition += " PInfo_Number LIKE '%" + ValueHandler.GetStringValue(tb_PInfo_Number.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (tb_PInfo_Pwd.Text.Trim() != "")
            {
                strCondition += " PInfo_Pwd LIKE '%" + ValueHandler.GetStringValue(tb_PInfo_Pwd.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            } 
            //通过选中的节点，获取产品大类信息
            if (this.tv_Menu.SelectedNodeIDArray.Length > 0)
            {
                string pid = this.tv_Menu.SelectedNodeIDArray[0];
                ExtAspNet.TreeNode tn= tv_Menu.FindNode(pid);
                string ids = "(" + pid + ",";
                GetSubIds(tn.Nodes, ref ids);
                ids = ids.Substring(0, ids.Length - 1) + ") ";
                strCondition += "PInfo_PSort_ID in " + ids + " AND ";
            }
            if (!string.IsNullOrEmpty(strCondition))
                strCondition = strCondition.Substring(0, strCondition.Length - 4);

        }
        private void GetSubIds(ExtAspNet.TreeNodeCollection tns, ref string ids)
        {
            for (int i = 0; i < tns.Count; i++)
            {
                ids += tns[i].NodeID + ",";
                if (tns[i].Nodes.Count > 0)
                    GetSubIds(tns[i].Nodes, ref ids);
            }
        }
        #endregion

        #region Property

        ProductSortOp SortManager = new ProductSortOp();
        ProductInfoOp Manager = new ProductInfoOp();
        #endregion
    }
}
